package com.ftqh.tutorial.web.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@Builder
public class ResponseEntity<T> implements Serializable {
    private ResponseStatus status;
    private final HttpStatus statusCode;
    private String message;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;
    private T data;

    public static <T> ResponseEntity<T> ok(T data) {
        ResponseEntityBuilder builder = ok(HttpStatus.OK);
        return builder.data(data).build();
    }
    public static <T> ResponseEntity<T> ok(T data, HttpStatus statusCode) {
        ResponseEntityBuilder builder = ok(statusCode);
        return builder.data(data).build();
    }
    public static ResponseEntityBuilder ok() {
        return responseEntityBuilder(ResponseStatus.SUCCESS);
    }

    public static ResponseEntityBuilder ok(HttpStatus statusCode) {
        return ok().statusCode(statusCode);
    }
    public static ResponseEntityBuilder error() {
        return responseEntityBuilder(ResponseStatus.FAIL);
    }
    public static ResponseEntityBuilder error(HttpStatus statusCode) {
        return error().statusCode(statusCode);
    }

    public static <T> ResponseEntity<T> notFound() {
        ResponseEntityBuilder builder = error(HttpStatus.NOT_FOUND);
        return builder.build();
    }
    public static <T> ResponseEntity<T> badRequest() {
        ResponseEntityBuilder builder = error(HttpStatus.BAD_REQUEST);
        return builder.build();
    }
    public static <T> ResponseEntity<T> internalServerError() {
        ResponseEntityBuilder builder = error(HttpStatus.INTERNAL_SERVER_ERROR);
        return builder.build();
    }

    public boolean getStatus() {
        return status.value();
    }

    public int getStatusCode(){
        return statusCode.value();
    }

    private static ResponseEntityBuilder responseEntityBuilder(ResponseStatus status) {
        return ResponseEntity.builder()
                .status(status)
                .timestamp(LocalDateTime.now());
    }
}
